/**
 * 基于actionlib的SimpleActionServer创建 action client
*/
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <actionlib/client/terminal_state.h>

#include "learn_actionlib/FibonacciAction.h"

int main(int argc, char **argv) {
  ros::init(argc, argv, "test_fibonacci");

  // create the action client
  // true causes the client to spin its own thread
  actionlib::SimpleActionClient<learn_actionlib::FibonacciAction> ac(
      "fibonacci", true);

  ROS_INFO("Waiting for action server to start.");
  // wait for the action server to start
  ac.waitForServer();  // will wait for infinite time

  ROS_INFO("Action server started, sending goal.");
  // send a goal to the action
  learn_actionlib::FibonacciGoal goal;
  goal.order = 20;
  ac.sendGoal(goal);

  // wait for the action to return
  bool finished_before_timeout = ac.waitForResult(ros::Duration(30.0));

  if (finished_before_timeout) {
    actionlib::SimpleClientGoalState state = ac.getState();
    ROS_INFO("Action finished: %s", state.toString().c_str());
    // auto result = ac.getResult();
    // ROS_INFO("Result sequence:");
    // for (int i = 0; i < result->sequence.size(); i++) {
    //   ROS_INFO("sequece[%d]:%d", i, result->sequence.at(i));
    // }
  } else
    ROS_INFO("Action did not finish before the time out.");

  // exit
  return 0;
}